using System;
using System.Collections.Generic;
using HtmlAgilityPack;
using resharperupdate.core.dto;
using resharperupdate.core.dto.impl;

namespace resharperupdate.core.services.impl
{
    public class NightlyBuildParser : INightlyBuildParser
    {
        public IBuild parse(string html)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var build = new Build();
            build.Date =            
                doc.DocumentNode.SelectSingleNode("//html/body/div[2]/div[2]/div[2]/table/tbody/tr[2]/td").InnerText.Trim();
            build.Number =
                Convert.ToInt32(
                    doc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[2]").InnerText);
            build.Url =
                doc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[3]/a").Attributes["href"].Value;
            build.Rating = process_rating(doc);
            build.Comments =
                doc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[5]").InnerHtml.Trim();
            build.Status = process_status(build.Comments);
            return build;
        }

        public IEnumerable<IBuild> for_list(string html)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//html/body/div[2]/div[2]/div[2]/table/tbody/tr");
            foreach (HtmlNode node in nodes)
            {
                HtmlNode date = node.SelectSingleNode("td[1]");
                if (date != null)
                {
                    yield return new Build
                                     {
                                         Date = date.InnerText.Trim(),
                                         Number = Convert.ToInt32(node.SelectSingleNode("td[2]").InnerText),
                                         Url = node.SelectSingleNode("td[3]/a").Attributes["href"].Value,
                                         Rating = process_rating(node.SelectNodes("td[4]/table/tr/td[7]/img")),
                                         Comments = node.SelectSingleNode("td[5]").InnerHtml.Trim(),
                                         Status = process_status(node.SelectSingleNode("td[5]").InnerHtml.Trim())
                                     };
                }
            }
        }

        BuildStatus process_status(string status)
        {
            if (status.Contains("<font color=\"red\">"))
                return BuildStatus.Error;
            if (status.Contains("<font color=\"green\">"))
                return BuildStatus.Good;
            return BuildStatus.Normal;
        }

        public decimal process_rating(HtmlDocument doc)
        {
            HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("/html/body/div[2]/div[2]/div[2]/table/tbody/tr[2]/td[4]/table/tr/td[7]/img");
            return process_rating(collection);
        }

        public decimal process_rating(HtmlNodeCollection collection)
        {
            decimal total = 0m;
            if (collection != null)
            {
                foreach (var img in collection)
                {
                    var attribute = img.Attributes["class"].Value;
                    if (attribute == "on")
                        total += 1m;
                    else if (attribute == "half")
                        total += 0.5m;
                }
            }
            return total;
        }
    }
}